﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConditionJudge.Question;

namespace ConditionJudge.Answer
{
    class Bob : IConditionJudge
    {
        public string Judgement(string expression, int x)
        {

            expression = '(' + expression + ')';

            int cnt = 0;

            foreach (var c in expression)
            {
                if (c == '(')
                {
                    cnt++;
                }
                if (c == ')')
                {
                    cnt--;
                }
                if (cnt < 0)
                    return "FF";
            }
            if(cnt > 0)
                return "FF";


            expression = expression.Replace(" ", string.Empty);
            Stack<string> stack = new Stack<string>();
            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] == '(')
                {
                    stack.Push(string.Empty);
                }
                else if (expression[i] == ')')
                {
                    string cur = stack.Pop();
                    if (!IsExpressionValid(cur))
                    {
                        return "FF";
                    }
                    else
                    {
                        string next = stack.Count == 0 ? string.Empty : stack.Pop();
                        next = next + OperateExpression(cur, x);
                        stack.Push(next);
                    }


                }
                else
                {
                    string cur = stack.Pop();
                    cur = cur + expression[i];
                    stack.Push(cur);
                }
            }

            return Convert.ToBoolean(GetExpressionValue(stack.Pop(), 0)) == true ? "TT" : "TF";
            


        }

        private bool OperateExpression(string cur, int x)
        {
            var strings = cur.Split(new string[] { "||", "&&" }, StringSplitOptions.RemoveEmptyEntries);
            List<bool> res = new List<bool>();
            for (int i = 0; i < strings.Length; i++)
            {
                res.Add(GetExpressionValue(strings[i], x));
            }
            if (cur.IndexOf("&&") >= 0)
            {
                if (res.All(m => m))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (cur.IndexOf("||") >= 0)
            {
                if (res.Any(m => m))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }



            return true;

        }

        private bool GetExpressionValue(string p, int x)
        {
            string[] strs = p.Split(new string[] { ">", "<", "=", "<=", ">=", "!=" }, StringSplitOptions.RemoveEmptyEntries);
            if (strs.Length > 1)
            {
                int y = Convert.ToInt32(strs[1]);

                if (p.Contains("!="))
                {
                    return x != y;
                }
                else if (p.Contains(">="))
                {
                    return x >= y;
                }
                else if (p.Contains("<="))
                {
                    return x <= y;
                }
                else if (p.Contains("="))
                {
                    return x == y;
                }
                else if (p.Contains("<"))
                {
                    return x < y;
                }
                else //if (p.Contains(">"))
                {
                    return x > y;
                }
            }
            else
            {
                return Convert.ToBoolean(strs[0]);
            }    

        }

        private bool IsExpressionValid(string cur)
        {

            if (cur.IndexOf("||") == 1 || cur.IndexOf("&&") == 1)
            {
                return false;
            }
            if (cur.IndexOf("||") != -1 && cur.IndexOf("&&") != -1)
            {
                return false;
            }

            return true;
        }

    }
}
